package com.hucheng.goods.utils;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-08-22
 * @Description: 类描述
 **/
public class HtmlTemplateUtils {
    private final static TemplateEngine templateEngine = new TemplateEngine();

    /**
     * 使用Thymeleaf渲染HTML
     *
     * @param template HTML模版
     * @param params   参数
     * @return 渲染后的HTML
     */
    public static void render(String template, Map<String, Object> params) {
        Context context = new Context();
        context.setVariables(params);
        FileWriter write = null;
        try {
            String path = System.getProperty("user.dir");
            write = new FileWriter(path + "/result.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        templateEngine.process(template, context, write);
    }
}
